$(document).ready(function() {
   $("#noticetable-modify").hide();
   $(document).on('click','.button-container-notice', function (e) {
    console.log("我点击了notice的修改按钮哦！")
        var time = $(this).attr('id')
        console.log(time)
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/notice/modify',
            data: {"time":time},
            dataType: "json",
            success: function (data) {
                    let $row = $('<tr>');
                    let $zero = $('<td>'),
                        $first = $('<td>'),
                        $second = $('<td>');

                    $zero.text(data[0].time).appendTo($row);
                    $zero.addClass("remake");
                    $first.text(data[0].title).appendTo($row);
                    $first.addClass("remake");
                    $second.text(data[0].content).appendTo($row);
                    $second.addClass("remake");

                    $row.appendTo('#modifyTable-notice tbody');

                    var remake = document.getElementsByClassName("remake");
                    for (let i = 0; i < remake.length; i++) {
                        remake[i].onclick = () => {
                            remake[i].style.display = "none";
                            var para = document.createElement("input");
                            var ctd = document.createElement("td");
                            para.value = remake[i].innerHTML;
                            remake[i].parentNode.insertBefore(ctd, remake[i]);
                            ctd.appendChild(para, ctd);
                            para.focus();
                            para.onblur = () => {
                                remake[i].innerHTML = para.value;
                                remake[i].parentNode.removeChild(ctd);
                                remake[i].style.display = ""
                            }
                        }
                    }
            }
        });
        $("#noticetable-modify").show();
    });

$(document).on('click','.button-delete-notice', function (e) {
        console.log("我点击了notice删除按钮！")
        var time = $(this).attr('name')
        console.log(name)
        e.preventDefault();
        $.ajax({
            type: 'DELETE',
            url: '/notice/delete',
            data: {"time":time},
            dataType: "json",
            success: function (data) {
                $('tbody tr').remove();
                $.each(data, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.time).appendTo($row);
                    $('<td>').text(item.title).appendTo($row);
                    $('<td>').text(item.content).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container-notice");
                    $btn_modify.attr('id',item.time)
                    $btn_delete.addClass("button-delete-notice");
                    $btn_delete.attr('name',item.time)
                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);
                    $cell.appendTo($row);

                    $row.appendTo('#notice-table tbody');
                    });
            }
        });
        $("noticetable-modify").hide();
    });

    $(document).on('click','.cancel-button-notice', function (e) {
        console.log("我点击了notice返回按钮！")
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/notice/returnNotice',
            dataType: "json",
            success: function (data) {
                $('tbody tr').remove();
                $.each(data, function (index, item) {
                    console.log(item.title);

                    let $row = $('<tr>');
                    $('<td>').text(item.time).appendTo($row);
                    $('<td>').text(item.title).appendTo($row);
                    $('<td>').text(item.content).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container-notice");

                    $btn_modify.attr('id',item.time)

                    $btn_delete.addClass("button-delete-notice");
                    $btn_delete.attr('name',item.time)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#notice-table tbody');
                    });
            }
        });
        $("#noticetable-modify").hide();
    });

    $(".confirm-button-notice").click(function (e) {
        e.preventDefault();
        $row = document.getElementById("modifyTable-notice");
        var data = [];
        for (var i = 0, rows = $row.rows.length; i < rows; i++) {
            for (var j = 0, cells = $row.rows[i].cells.length; j < cells; j++) {
                if (!data[i]) {
                    data[i] = [];
                }
                data[i][j] = $row.rows[i].cells[j].innerHTML;
            }
        }
        $.ajax({
            type: 'PUT',
            url: '/notice/modifyResult',
            data:{
                "time":data[1][0],
                "title":data[1][1],
                "content":data[1][2]
            },
            success: function (data) {
                $('tbody tr').remove();
                $("#noticetable-modify").hide();
                var jsonObj = JSON.parse(data);
                $.each(jsonObj, function (index, item) {
                    console.log(item.username);
                    let $row = $('<tr>');
                    $('<td>').text(item.time).appendTo($row);
                    $('<td>').text(item.title).appendTo($row);
                    $('<td>').text(item.content).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container-notice");

                    $btn_modify.attr('id',item.time)


                    $btn_delete.addClass("button-delete-notice");
                    $btn_delete.attr('name',item.time)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);
                    $cell.appendTo($row);
                    $row.appendTo('#notice-table tbody');

                    });
                }
            })
        });
});
